package com.heyun.controller;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heyun.bean.HeyunCustomer;
import com.heyun.bean.HeyunMerchandise;
import com.heyun.bean.HeyunOrder;
import com.heyun.bean.HeyunOrderMerchandise;
import com.heyun.bean.HeyunShoppingCart;
import com.heyun.service.HeyunOrderService;
import com.heyun.service.impl.HeyunOrderServiceImpl;
/**
 * @author zll
 * @Description 查询所有员工controller
 * @Company 
 * @date 2017年12月14日
 */
@Controller
@RequestMapping("orderManagement/theOrderOf")
public class OrderController {
	@Autowired
	private HeyunOrderServiceImpl heyunOrderService;
	/**
	 * 查看客户信息
	 * @param orCusId
	 */
	@RequestMapping("/seeTheCustomer")
	@ResponseBody
	public Map<String,Object> getOrderManagement(int cusId){
		System.out.println(cusId+"=============");
		HeyunOrder heyunOrder = heyunOrderService.getOrderManagement(cusId);
		List<HeyunOrder> listOrCusId = heyunOrderService.listOrCusId(cusId);
		Map<String,Object> map = new HashMap<String, Object>();
		List<List<HeyunOrderMerchandise>> list =new ArrayList<List<HeyunOrderMerchandise>>();
		for(int i=0;i<listOrCusId.size();i++){
			int orCusId = listOrCusId.get(i).getOrCusId();
			List<HeyunOrderMerchandise> HeyunOrderMerchandise = heyunOrderService.getOrderMerchandiseOrCusId(orCusId);
			list.add(HeyunOrderMerchandise);
		}
		map.put("heyunOrder", heyunOrder);
		map.put("listOrCusId", listOrCusId);
		map.put("listMerchandise", list);
		return map;
	}
	/**
	 * 待下订单部分的首页默认显示的
	 */
	@RequestMapping("/AutomaticQuery")
	@ResponseBody
	public Map<String,Object> getOrderList(String getCustomer,String getConditions,Integer currentpage){
		HeyunOrder heyunOrder = new HeyunOrder();
		System.out.println(getCustomer+"++++++++");
		if(getCustomer.equals("客户姓名")){
			System.out.println(getConditions+"getConditions客户姓名");
			heyunOrder.setOrName(getConditions);
		}else if(getCustomer.equals("联系电话")){
			heyunOrder.setOrPhone(getConditions);
			System.out.println(getConditions+"getConditions联系电话");
		}
		Map<String,Object> map = new HashMap<>();
		Page page = PageHelper.startPage(currentpage,5);
		List<HeyunOrder> listHeyunOrderUser = heyunOrderService.getOrderUser(heyunOrder);
		int pages = page.getPages();//总页数
		for(int i=0;i<listHeyunOrderUser.size();i++){
			double totalAmountPerUser=0.00;
			int totalConsumptionFrequency=0;
			List<HeyunOrder> listHeyunOrderMoney = heyunOrderService.getOrderMoney(listHeyunOrderUser.get(i).getCusId());
			for(int j=0;j<listHeyunOrderMoney.size();j++){
				totalAmountPerUser +=Double.valueOf(listHeyunOrderMoney.get(j).getOrTotalConsumptionAmount());
				totalConsumptionFrequency+=1;
			}
			listHeyunOrderUser.get(i).setTotalAmountPerUser(totalAmountPerUser);
			listHeyunOrderUser.get(i).setTotalConsumptionFrequency(totalConsumptionFrequency);
		}
		map.put("pages", pages);
		map.put("listHeyunOrderUser", listHeyunOrderUser);
		return map;
	}
	/**
	 * 添加订单
	 */
	@RequestMapping("/addOrder")
	@ResponseBody
	public void addHeyunOrder1(HeyunOrder heyunOrder,HttpServletRequest request,MultipartFile orInvoiceInformationFile){
		String realPath=request.getServletContext().getRealPath("/invoicePicture");
		//System.out.println("realPath"+realPath);
		String orInvoiceInformation=realPath+"\\"+orInvoiceInformationFile.getOriginalFilename();
		//System.out.println("uploadPath="+orInvoiceInformation);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		heyunOrder.setOrDate(new Date());
		/*String empName = request.getParameter("empRealName");
		int empId = Integer.parseInt(request.getParameter("empId"));*/
		String empName = "陈晨";
		int empId = 3;
		heyunOrder.setEmpId(empId);
		heyunOrder.setEmpName(empName);
		String deptName = heyunOrderService.getDaptOrder(empId);
		heyunOrder.setDeptName(deptName);
		if(orInvoiceInformationFile.getOriginalFilename().equals("")){
			heyunOrder.setOrInvoiceInformation("");
		}else{
			heyunOrder.setOrInvoiceInformation(orInvoiceInformation);
			try {
				IOUtils.copy(orInvoiceInformationFile.getInputStream(), new FileOutputStream(orInvoiceInformation));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		heyunOrderService.addOrder(heyunOrder);
		List<HeyunShoppingCart> listShoppingCart = heyunOrderService.getShoppingCart();
		for(int i=0;i<listShoppingCart.size();i++){
			HeyunOrderMerchandise heyunOrderMerchandise = new HeyunOrderMerchandise();
			heyunOrderMerchandise.setOrCusId(heyunOrder.getOrCusId());
			heyunOrderMerchandise.setMerId(listShoppingCart.get(i).getMerId());
			heyunOrderMerchandise.setOrMerModelNumber(listShoppingCart.get(i).getShoMerModelNumber());
			heyunOrderMerchandise.setOrMerName(listShoppingCart.get(i).getShoMerName());
			heyunOrderMerchandise.setOrMerPrice(listShoppingCart.get(i).getShoMerPrice());
			heyunOrderMerchandise.setOrNumber(listShoppingCart.get(i).getShoNumber());
			heyunOrderService.addHeyunOrderMerchandiseService(heyunOrderMerchandise);
		}
		heyunOrderService.delHeyunShoppingCart();
		//return "order";
	}
	/**
	 *向购物车中查询
	 */
	@RequestMapping("getShoppingCart")
	@ResponseBody
	public Map<String,Object> getShoppingCart(){
		List<HeyunShoppingCart> listShoppingCart = heyunOrderService.getShoppingCart();
		double toble = 0.00;
		for(int i=0;listShoppingCart.size()>i;i++){
			toble += listShoppingCart.get(i).getShoMerPrice();
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("listShoppingCart",listShoppingCart);
		map.put("toble",toble);
		return map;
	}
	/**
	 * 向购物车添加商品
	 */
	@RequestMapping("/addShoppingCart")
	@ResponseBody
	public String addShoppingCart(Integer merId, String merName,String merModelNumber,Integer number,double merPrice,Integer merStock){
		HeyunShoppingCart shopping = new HeyunShoppingCart();
		shopping.setMerId(merId);
		shopping.setShoMerName(merName);
		shopping.setShoMerModelNumber(merModelNumber);
		shopping.setShoNumber(number);
		shopping.setShoMerPrice(merPrice);
		shopping.setShoMerStock(merStock);
		int add = heyunOrderService.addShoppingCart(shopping);
		return add+"";
	}
	/**
	 * 根据ID查询客户信息,根据用户名查询用户总消费金额和总消费次数
	 */
	@RequestMapping("/orderOfService")
	@ResponseBody
	public Map<String,Object> getHeYunCustomer(String userName){
		HeyunCustomer HeYunCustomer =heyunOrderService.getHeyunCustomer(userName);
		Map<String,Object> listHeyunOrder = heyunOrderService.listTotalConsumption(userName);
		listHeyunOrder.put("HeYunCustomer", HeYunCustomer);
		return listHeyunOrder;
	}
	/**
	 * 查询所有商品
	 */
	@RequestMapping(value="/queryAllProducts")
	@ResponseBody
	public Map<String,Object> listMerchandise(){
		List<HeyunMerchandise> listMerchandise = heyunOrderService.listMerchandise();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("listMerchandise", listMerchandise);
		return map;
	}
}
